use serde::{Deserialize, Serialize};
use serde_json::Value;

/// 操作结果结构体
/// 对应C#版本OperateResult
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct OperateResult {
    pub success: bool,
    pub data: Value,
    pub msg: String,
    pub code: u16,
}

impl OperateResult {
    /// 创建失败结果
    pub fn fail(error: &str) -> Self {
        Self {
            success: false,
            data: Value::Null,
            msg: error.to_string(),
            code: 500,
        }
    }

    /// 创建失败结果（带数据）
    pub fn fail_with_data(error: &str, data: Value) -> Self {
        Self {
            success: false,
            data,
            msg: error.to_string(),
            code: 500,
        }
    }

    /// 创建成功结果
    pub fn success(data: Value) -> Self {
        Self {
            success: true,
            data,
            msg: "成功".to_string(),
            code: 200,
        }
    }

    /// 转换为JSON响应
    pub fn to_json_response(&self) -> axum::Json<Self> {
        axum::Json(self.clone())
    }
}

/// 泛型操作结果结构体
/// 对应C#版本OperateResult<T>
#[derive(Debug, Serialize, Deserialize)]
pub struct OperateResultT<T> {
    pub success: bool,
    pub data: T,
    pub msg: String,
    pub code: u16,
}

impl<T> OperateResultT<T> {
    /// 创建成功结果
    pub fn success(data: T) -> Self {
        Self {
            success: true,
            data,
            msg: "成功".to_string(),
            code: 200,
        }
    }

    /// 创建失败结果
    pub fn fail(error: &str) -> Self
    where
        T: Default,
    {
        Self {
            success: false,
            data: T::default(),
            msg: error.to_string(),
            code: 500,
        }
    }
}